﻿using System;
using System.Web.UI;
using Ninject;
using Legazpi.Payroll.BusinessLogic.Services.Interfaces;
using System.Web.UI.WebControls;
using Legazpi.Payroll.DataAccess.DataObjects;
using Web.Codes;

namespace Legazpi.Payroll.Web.Admin
{
    public partial class SSSChartTable : BasePage
    {
        [Inject]
        public ISSSTableService _sssTableService { get; set; }

        public bool IsSuccess { get; set; }
        public string NotifyText { get; set; }

        #region Implementations
        void BindGrid()
        {
            repeater.DataSource = _sssTableService.GetAll();
            repeater.DataBind();
        }

        void Delete(int id)
        {
            _sssTableService.Delete(id);
            IsSuccess = _sssTableService.Save();
            NotifyText = "Successfully Deleted.";
            BindGrid();
        }

        void Save(int id, RepeaterItem item)
        {
            var sssTable = _sssTableService.GetById(id);

            var startRangeTxt = ((TextBox)item.FindControl("LowTxtItem")).Text;
            var endRangeTxt = ((TextBox)item.FindControl("HighTxtItem")).Text;
            var erTxtItem = ((TextBox)item.FindControl("ERTxtItem")).Text;
            var eeTxtItem = ((TextBox)item.FindControl("EETxtItem")).Text;
            var baseSalaryTxt = ((TextBox)item.FindControl("BaseTxtItem")).Text;
            var totalTxtItem = ((TextBox)item.FindControl("TotalTxtItem")).Text;


            double startRange;
            double endRange;
            double er;
            double ee;
            double baseSalary;
            double total;

            double.TryParse(startRangeTxt, out startRange);
            double.TryParse(endRangeTxt, out endRange);
            double.TryParse(erTxtItem, out er);
            double.TryParse(eeTxtItem, out ee);
            double.TryParse(baseSalaryTxt, out baseSalary);
            double.TryParse(totalTxtItem, out total);

            sssTable.StartRange = startRange;
            sssTable.EndRange = endRange;
            sssTable.Er = er;
            sssTable.Ee = ee;
            sssTable.Base = baseSalary;
            sssTable.Total = total;

            _sssTableService.Update(sssTable);
            IsSuccess = _sssTableService.Save();
            NotifyText = "Successfully Updated.";
            BindGrid();
        }

        void Add()
        {
            var sssTable = new SSSTable();

            double startRange;
            double endRange;
            double er;
            double ee;
            double baseSalary;
            double total;


            double.TryParse(this.LowRangeTxt.Text, out startRange);
            double.TryParse(this.HighRangeTxt.Text, out endRange);
            double.TryParse(this.ERTxt.Text, out er);
            double.TryParse(this.EETxt.Text, out ee);
            double.TryParse(this.BaseTxt.Text, out baseSalary);
            double.TryParse(this.TotalTxt.Text, out total);

            sssTable.StartRange = startRange;
            sssTable.EndRange = endRange;
            sssTable.Er = er;
            sssTable.Ee = ee;            
            sssTable.Base = baseSalary;
            sssTable.Total = total;

            _sssTableService.Add(sssTable);
            IsSuccess = _sssTableService.Save();
            NotifyText = "Successfully Added New Record.";
            BindGrid();
        }
        #endregion

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
                BindGrid();
        }

        protected void repeater_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            int intTemp = 0;
            if (int.TryParse((string)e.CommandArgument, out intTemp))
            {
                switch (e.CommandName.ToLower())
                {
                    case Constants.Save_Command:
                        Save(intTemp, e.Item);
                        break;
                    case Constants.Delete_Command:
                        Delete(intTemp);
                        break;
                }
            }
        }

        protected void AddNewBtn_Click(object sender, EventArgs e)
        {
            Add();
            if (IsSuccess)
            {
                this.LowRangeTxt.Text = string.Empty;
                this.HighRangeTxt.Text = string.Empty;
                this.ERTxt.Text = string.Empty;
                this.EETxt.Text = string.Empty;
                this.BaseTxt.Text = string.Empty;
                this.TotalTxt.Text = string.Empty;
            }
        }
    }
}